package com.example.administrator.myapplication.activity.billboardSecondActivity;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.billboardSecondActivity.bean.BillboardSecondEntity.BillboardSecondEntity;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentBillboardSecondBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BillboardSecondFragment extends Fragment implements Callback<BillboardSecondEntity> {
      private FragmentBillboardSecondBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;
    private  int page =1;

    public BillboardSecondFragment() {
        // Required empty public constructor
    }

    public static BillboardSecondFragment newInstance(String key) {
        Bundle args = new Bundle();
        args.putString("key",key);
        BillboardSecondFragment fragment = new BillboardSecondFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_billboard_second, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       binding = DataBindingUtil.getBinding(view);
      aboutRecycler();
    }

    private void aboutRecycler() {
        String key = getArguments().getString("key");
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), R.layout.billboard_second_item, BR.billboardSecondData);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getBillboardSecondEntity(key,page,20).enqueue(this);
    }

    @Override
    public void onResponse(Call<BillboardSecondEntity> call, Response<BillboardSecondEntity> response) {
         adapter.addAll(response.body().getList());
    }

    @Override
    public void onFailure(Call<BillboardSecondEntity> call, Throwable t) {
        Toast.makeText(getContext(), "BillboardSecondFragment"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
